#include "transwidget.h"
#include <QHBoxLayout>
#include <QStringList>
#include <QDebug>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QRandomGenerator>
#include <QCryptographicHash>
#include <QJsonParseError>
#include <QJsonObject>
#include <QJsonArray>
#include <QMessageBox>
#include <QMessageBox>
TransWidget::TransWidget(QWidget *parent) : QWidget(parent)
{
    pTransButton = new QPushButton("翻译");
    pChangeButton = new QPushButton("交换");
    pLeftTextEdit = new QTextEdit;
    pRightTextEdit = new QTextEdit;
    pToLabel = new QLabel("==>");

    QHBoxLayout *pTopLayout = new QHBoxLayout;
    pTopLayout->addWidget(pLeftTextEdit);
    pTopLayout->addWidget(pToLabel);
    pTopLayout->addWidget(pRightTextEdit);

    pSourceCombox = new QComboBox;
    pSourceCombox->addItem("中文");

    pDestCombox = new QComboBox;
    pDestCombox->addItem("english");

    pChangeButton = new QPushButton("<=>");
    pChangeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
    pTransButton = new QPushButton("翻译");
    pBelowLayout = new QHBoxLayout;
    pBelowLayout->addWidget(pSourceCombox);
    pBelowLayout->addWidget(pChangeButton);
    pBelowLayout->addWidget(pDestCombox);
    pBelowLayout->addWidget(pTransButton);

    QVBoxLayout *pMainlayout = new QVBoxLayout;
    pMainlayout->addLayout(pTopLayout);
    pMainlayout->addLayout(pBelowLayout);
    setLayout(pMainlayout);

    connect(pChangeButton, &QPushButton::clicked, this, &TransWidget::sourceChangeSlot);
    connect(pTransButton, &QPushButton::clicked, this, &TransWidget::transSlot);
}


void TransWidget::sourceChangeSlot(void)
{
    pBelowLayout->removeWidget(pDestCombox);
    pBelowLayout->removeWidget(pSourceCombox);
    pBelowLayout->insertWidget(0, pDestCombox);
    pBelowLayout->insertWidget(2, pSourceCombox);

    QComboBox *pTmpCombox = pSourceCombox;
    pSourceCombox = pDestCombox;
    pDestCombox = pTmpCombox;
    qDebug() << pSourceCombox->currentText() << pDestCombox->currentText();

}


void TransWidget::transSlot()
{
    QUrl url;
    url.setUrl("http://api.fanyi.baidu.com/api/trans/vip/translate?");

    QNetworkRequest request;
    request.setUrl(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));

    QString appIdString("20200327000407062");
    QString queryString = pLeftTextEdit->toPlainText();
    QString saltString = QString::number(QRandomGenerator::global()->generate());
    QString secretKeyString("80DmptQpWni6qntXygY5");
    QString signString = appIdString + queryString + saltString + secretKeyString;

    signString = QCryptographicHash::hash(signString.toUtf8(), QCryptographicHash::Md5).toHex();
    QByteArray byteArr;
    byteArr.append("q=");
    byteArr.append(queryString);
    byteArr.append("&from=zh&to=en&");
    byteArr.append("appid=");
    byteArr.append(appIdString + "&salt=");
    byteArr.append(saltString + "&sign=");
    byteArr.append(signString);

    pNetworkAccessManager = new QNetworkAccessManager(this);
    pNetworkAccessManager->post(request,byteArr);
    pRightTextEdit->clear();
    connect(pNetworkAccessManager, &QNetworkAccessManager::finished,this, &TransWidget::transResultSlot);
}

void TransWidget::transResultSlot(QNetworkReply* reply)
{
    QNetworkReply::NetworkError err = reply->error();
    if(err != QNetworkReply::NoError)
    {
        QMessageBox::information(this, "出错", QString("Failed:") + reply->errorString());
    }
    else
    {
        QJsonParseError json_error;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &json_error);
        if(jsonDoc.isObject())
        {
            QJsonObject obj = jsonDoc.object();
            QJsonArray jsonArr = obj.take("trans_result").toArray();
            for(uint16_t i = 0; i < jsonArr.size(); i++)
            {
                pRightTextEdit->append(jsonArr.at(i).toObject().take("dst").toString());
            }
        }
    }
    pNetworkAccessManager->deleteLater();

}

